package com.hthy.futures.ui.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hthy.futures.AppContext;
import com.hthy.futures.R;
import com.hthy.futures.api.ST_TradeApi;
import com.hthy.futures.base.BaseFragment;
import com.hthy.futures.bean.DeclareMoney;
import com.hthy.futures.ui.activity.PullPayActivity;
import com.hthy.futures.utils.DialogUtil;
import com.hthy.futures.utils.ErrorUtils;
import com.hthy.futures.utils.LogUtils;
import com.hthy.futures.utils.ShaUtils;
import com.hthy.futures.utils.StringUtils;
import com.hthy.futures.view.EditTextWithDel2;
import com.vilyever.socketclient.SocketClient;

import net.hthy.bean.Trade;

import butterknife.Bind;

/**
 * 入金申请
 * Author:  Mr.bingley
 * Version:
 * Date:  2017/3/6
 */

public class PullMoneyFragment extends BaseFragment {
    @Bind(R.id.login_user)
    TextView mLoginUser;
    @Bind(R.id.et_fundpwd)
    EditTextWithDel2 mEtFundpwd;
    @Bind(R.id.et_fund_money)
    EditTextWithDel2 mEtFundMoney;
    @Bind(R.id.et_note_content)
    EditText mEtNoteContent;
    @Bind(R.id.bt_post)
    Button mBtPost;

    @Bind(R.id.et_licese_number)
    EditTextWithDel2 mEtLiceseNumber;
    @Bind(R.id.et_bank_code)
    EditTextWithDel2 mEtBankCode;
    @Bind(R.id.et_bank_where)
    EditTextWithDel2 mEtBankWhere;
    @Bind(R.id.et_branch_name)
    EditTextWithDel2 mEtBranchName;

    private String mLiceseNumber;
    private String mBankCode;
    private String mBankWhere;
    private String mBranceName;

    private String mFundpwd;
    private String mFundMoney;
    private String mMoney;
    private String mNoteContent;

    @Override
    protected int getLayoutId() {
        return R.layout.frag_pull_money;
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        mLoginUser.setText(AppContext.accountName);

        mBtPost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkValide()) {
                    return;
                }
                DeclareMoney declareMoney = new DeclareMoney();
                declareMoney.de_userId = AppContext.accountId+"";   // 這個不需要填，好奇怪
                declareMoney.de_fundPwd = ShaUtils.getSha1(mFundpwd);
                declareMoney.de_out_in_money= mMoney;
                declareMoney.de_operType = '1';

          /*      declareMoney.de_licence_number = mLiceseNumber;

                Map<String, String> otherMapParas = new HashMap<String, String>();
                otherMapParas.put(getString(R.string.Bank_code), mBankCode);
                otherMapParas.put(getString(R.string.bank_city), mBankWhere);
                otherMapParas.put(getString(R.string.bank_branch), mBranceName);
                String gsonstr = AppContext.createGson().toJson(otherMapParas);  //字符串转json

                declareMoney.de_other_paramters = gsonstr;*/

                socketClient.sendData(ST_TradeApi.moneyPushPullDeclaretrade(declareMoney));
                DialogUtil.showDialog(getActivity());
            }
        });




    }

    public boolean checkValide() {
        mFundpwd = mEtFundpwd.getText().toString().trim();
        mMoney = mEtFundMoney.getText().toString().trim();
        mNoteContent = mEtNoteContent.getText().toString().trim();


        mLiceseNumber = mEtLiceseNumber.getText().toString().trim();
        mBankCode = mEtBankCode.getText().toString().trim();
        mBankWhere = mEtBankWhere.getText().toString().trim();
        mBranceName = mEtBranchName.getText().toString().trim();

        if (StringUtils.isEmpty(mFundpwd)) {
            showToast(R.string.fundpwd);
            return true;
        }
        if (StringUtils.isEmpty(mMoney)) {
            showToast(R.string.fund_money);
            return true;
        }
        /*if (StringUtils.isEmpty(mNoteContent)) {
            showToast(R.string.note_content);
            return true;
        }*/
        /*if (StringUtils.isEmpty(mLiceseNumber)) {
            showToast(R.string.tip_license_number);
            return true;
        }
        if (StringUtils.isEmpty(mBankCode)) {
            showToast(R.string.tip_bank_code);
            return true;
        }
        if (StringUtils.isEmpty(mBankWhere)) {
            showToast(R.string.tip_bank_where);
            return true;
        }
        if (StringUtils.isEmpty(mBranceName)) {
            showToast(R.string.tip_brance_name);
            return true;
        }
*/
        return false;
    }

    @Override
    protected void onResponse(SocketClient client, String responMes, byte[] subBytes) {
        super.onResponse(client, responMes, subBytes);
        LogUtils.e("PllMoneyFragment", responMes);
        try {
             if (responMes.indexOf("2070") != -1) {

                Trade.CallbackPayResponseMsg transResponseMsg = Trade.CallbackPayResponseMsg.parseFrom(subBytes);
                int iErrorCode = transResponseMsg.getResponseMsg().getIErrorCode();
                if (0 == iErrorCode) {
                    // 得到一个url后，跳转到一个webview
                    startActivity(new Intent(getActivity(), PullPayActivity.class).putExtra(getString(R.string.pullurl),transResponseMsg.getSsCallbackAddress()));
                } else {
                    ErrorUtils.sendErrorMes(getActivity(), iErrorCode + "");
                }
                DialogUtil.dimissDialog();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
